class Solution(object):
    def myAtoi(self, s):
        """
        :type s: str
        :rtype: int
        """
        #去掉空格
        s = s.strip(" ")
        sign_flag = 1
        num = 0
    
        for i in range(len(s)):
            if i == 0 and s[i] == "-":
                sign_flag = -1
            elif i == 0 and s[i] == "+":
                continue
            elif s[i] >= "0" and s[i] <= "9":
                num = num * 10 + int(s[i])
            else:
                break
        num = sign_flag * num
        max_int = 2 ** 31 - 1
        min_int = - 2 ** 31
        if num > max_int:
            num = max_int
        elif num < min_int:
            num = min_int
        return num
